#include "tj_drug_consume_dept.h"
#include "ui_tj_drug_consume_dept.h"

#include "pub_var.h"
#include <QSettings>
#include <QMessageBox>
#include <QFileDialog>
#include <QBuffer>

Tj_drug_consume_dept::Tj_drug_consume_dept(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Tj_drug_consume_dept)
{
    ui->setupUi(this);

    initial_finished = false;

    qDebug()<<"\n\nTj_drug_consume_dept initial...\n";

    //恢复当前窗口位置
    QSettings setIni("clin.ini", QSettings::IniFormat);
    QRect rct = setIni.value("windowState/tj_drug_consume_dept").toRect();
    this->setGeometry(rct);

    remote_path = setIni.value("database/host_name").toString();

    if(remote_path.isEmpty())
    {
        remote_path = setIni.value("database/m_hostname").toString();
    }

    remote_path = "//" + remote_path + "/pics/pics";

    //图片文件路径
    pic_path = pub::base_path + "/pics";
    QDir d = QDir(pic_path);

    if(!d.exists())
    {
        d.mkdir(pic_path);
    }

    //数据库连接
    qro = new QSqlQuery(pub::db_o);
    qrot = new QSqlQuery(pub::db_o);
    qrm = new QSqlQuery(pub::db_m);

    md = new QSqlQueryModel(this);
    md_sp = new QSqlQueryModel(this);
    md_sign = new QSqlQueryModel(this);
    md_pic = new QSqlQueryModel(this);

    sq = " select name, code from com_dictionary where type ='ITEMTYPE' and  VALID_STATE  = '1' order by sort_id ";
    qDebug()<<"提取药品类别："<<sq<<"\n";
    drug_type = pub::fillCombo(ui->cbb_drug_type, sq , -1, qrot);//

    sq = "select dept_name, dept_code from com_department where valid_state = '1' and regdept_flag = '1'";
    qDebug()<<"提取门诊科室："<<sq<<"\n";
    consume_depts = pub::fillCombo(ui->cbb_reci_dept, sq, -1);

    sq = "SELECT DISTINCT priv.role_dept, dept.dept_code FROM com_privileges priv "
         " join com_department dept on dept.dept_name = priv.role_dept"
         " where priv.type = 'dept'";
    qDebug()<<"提取工作人员科室："<<sq<<"\n";
    dept_sign = pub::fillCombo(ui->cbb_dept_sign, sq, -1);

    ui->cbb_reci_constrain->addItem("全部");
    pub::fillCombo(ui->cbb_reci_constrain, sq, 0, false);

    sq  = "select dept_name,dept_code from com_department where valid_state = '1'"
          " and dept_type in ('P') order by dept_type,dept_name";
    qDebug()<<"提取库存单位："<<sq<<"\n";
    store_depts = pub::fillCombo(ui->cbb_pha_dept, sq, -1);

    ui->cbb_pha_constrain->addItem("全部");
    pub::fillCombo(ui->cbb_pha_constrain, sq, 0, false);

    ui->cbb_doct_type->clear();
    qrm->exec("select name from com_dict_com where dic_type = '执业范围' order by ord");
    while(qrm->next())
    {
        ui->cbb_doct_type->addItem(qrm->value(0).toString());
    }
    ui->cbb_doct_type->setCurrentIndex(-1);

    sq = "select med_rec_item from ys_bz_e_print_rule"
         " where doct_type = 'ALL'"
         " order by print_seq";

    qrm->exec(sq);
    ui->lw_med_item_all->clear();
    while(qrm->next())
    {
        ui->lw_med_item_all->addItem(qrm->value(0).toString());
    }

    ui->tabWidget->setCurrentIndex(0);

    //检索系统当前用户查询权限
    sq = " select role_dept from com_privileges "
         " where code = '" + pub::usr_code + "'";
    QStringList roles;
    qrm->exec(sq);
    while(qrm->next())
    {
        roles << qrm->value(0).toString();
    }
    qDebug()<<"提取用户权限 : "<<sq;

    ui->tab_drug_dept->setEnabled(false);
    ui->tab_drug_privilege->setEnabled(false);
    ui->tab_sign_pic->setEnabled(false);
    ui->tab_patient_pic->setEnabled(false);
    ui->tab_med_rec->setEnabled(false);

    if(roles.indexOf("医生") > -1)
    {
        ui->tab_patient_pic->setEnabled(true);
    }

    if(roles.indexOf("管理员") > -1)
    {
        ui->tab_drug_dept->setEnabled(true);
        ui->tab_drug_privilege->setEnabled(true);
        ui->tab_sign_pic->setEnabled(true);
        ui->tab_patient_pic->setEnabled(true);
        ui->tab_med_rec->setEnabled(true);
    }
    initial_finished = true;
    qDebug()<<"Tj_drug_consume_dept initial finished.\n";
}

Tj_drug_consume_dept::~Tj_drug_consume_dept()
{
    delete ui;

    //delete md;
    //delete md_sp;
    delete qro;
    delete qrot;
    delete qrm;
}

void Tj_drug_consume_dept::on_pb_exit_clicked()
{
    close();
}

void Tj_drug_consume_dept::closeEvent(QCloseEvent *event)
{
    qDebug()<<"\n\nTj_drug_consume_dept triggered close event...\n";
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/tj_drug_consume_dept", QVariant(this->geometry()));

    emit destroyW( "tj_drug_consume_dept" );

}

void Tj_drug_consume_dept::on_pb_fresh_clicked()
{
    qDebug()<<"\n\non_pb_fresh_clicked()\n";
    sq = "SELECT "
         "  com_room.dept_name AS 处方诊室,"
         "  dict.name AS 药品类别,"
         "  com_dept.dept_name AS 执行药房,"
         "  PHA.ROOM_CODE, "
         "  PHA.DRUG_TYPE,  "
         "  PHA.DEPT_CODE"
         " FROM "
         " pha_sto_setdept pha"
         " LEFT JOIN com_department com_room ON pha.ROOM_CODE = com_room.dept_code "// -- 关联处方诊室
         " LEFT JOIN com_dictionary dict ON dict.type = 'ITEMTYPE' AND dict.VALID_STATE = '1' "
         "      AND dict.code = pha.drug_type "//         -- 关联药品类别字典
         " LEFT JOIN com_department com_dept ON pha.DEPT_CODE = com_dept.dept_code"//  -- 关联执行药房
         " where 1 = 1 %1 %2 "
         " order by com_room.dept_name, pha.drug_type";

    QString reci_dept = ui->cbb_reci_constrain->currentText();
    QString pha_dept = ui->cbb_pha_constrain->currentText();

    if( reci_dept == "全部")
    {
        reci_dept = "";
    }
    else
    {
        int pos = pub::searchStr(consume_depts, "^", reci_dept);
        if(pos == 0)
        {
            reci_dept = "";
        }
        else
        {
            reci_dept = " and pha.room_code = '"
                + pub::getStr(consume_depts, "^", pos + 1) + "'";
        }
    }

    if( pha_dept == "全部")
    {
        pha_dept = "";
    }
    else
    {
        int pos = pub::searchStr(store_depts, "^", pha_dept);
        if(pos == 0)
        {
            pha_dept = "";
        }
        else
        {
            pha_dept = " and pha.dept_code = '"
                + pub::getStr(store_depts, "^", pos + 1) + "'";
        }
    }

    sq = sq.arg(reci_dept)
            .arg(pha_dept);

    qDebug()<<"提取处方与药房关联："<<sq<<"\n";

    md->setQuery(sq, pub::db_o);

    ui->tv_drug_dept->setModel(md);

    int w = ui->tv_drug_dept->width();

    pub::setColumnWidth(ui->tv_drug_dept, "0.3^0.3^0.3^0^0^0", w);



}

void Tj_drug_consume_dept::on_tv_drug_dept_doubleClicked(const QModelIndex &index)
{
    on_tv_drug_dept_clicked(index);
}

void Tj_drug_consume_dept::on_tv_drug_dept_clicked(const QModelIndex &index)
{

    ui->cbb_reci_dept->setCurrentIndex(-1);
    ui->cbb_reci_dept->setCurrentText(index.siblingAtColumn(0).data().toString());
    ui->cbb_drug_type->setCurrentIndex(-1);
    ui->cbb_drug_type->setCurrentText(index.siblingAtColumn(1).data().toString());
    ui->cbb_pha_dept->setCurrentIndex(-1);
    ui->cbb_pha_dept->setCurrentText(index.siblingAtColumn(2).data().toString());
}

void Tj_drug_consume_dept::on_pb_delete_clicked()
{
    if(!ui->tv_drug_dept->currentIndex().isValid())
    {
        QMessageBox::information(this, "注意", "未指定有效关联关系！");
        return;
    }

    QString reci, store, type;
    reci = ui->tv_drug_dept->currentIndex().siblingAtColumn(3).data().toString();
    type = ui->tv_drug_dept->currentIndex().siblingAtColumn(4).data().toString();
    store = ui->tv_drug_dept->currentIndex().siblingAtColumn(5).data().toString();

    QMessageBox::StandardButton bt;
    QString msg = "确定要删除诊室 \"%1 ：%2\" 与 \"%3\" 的关联？";
    msg = msg.arg(ui->tv_drug_dept->currentIndex().siblingAtColumn(0).data().toString())
            .arg(ui->tv_drug_dept->currentIndex().siblingAtColumn(1).data().toString())
            .arg(ui->tv_drug_dept->currentIndex().siblingAtColumn(2).data().toString());
    bt = QMessageBox::question(this, "注意", msg);

    if(bt != QMessageBox::Yes)
    {
        return;
    }

    sq = " delete from pha_sto_setdept "
         " where room_code = '%1' "
         " and drug_type = '%2' "
         " and dept_code = '%3'";

    sq = sq.arg(reci).arg(type).arg(store);

    qDebug()<<"删除数据："<<sq<<"\n";

    qro->exec(sq);

    on_pb_fresh_clicked();

}

void Tj_drug_consume_dept::on_pb_save_clicked()
{

    if(ui->cbb_pha_dept->currentText().isEmpty()
            ||ui->cbb_reci_dept->currentText().isEmpty()
            ||ui->cbb_drug_type->currentText().isEmpty())
    {
        QMessageBox::information(this, "注意", "基础项目不可以空缺！");
        return;
    }

    QString reci, store, type;
    int pos;

    pos = pub::searchStr(consume_depts, "^", ui->cbb_reci_dept->currentText());
    if(pos == 0)
    {
        reci = "";
    }
    else
    {
        reci = pub::getStr(consume_depts, "^", pos + 1);
    }


    pos = pub::searchStr(store_depts, "^", ui->cbb_pha_dept->currentText());
    if(pos == 0)
    {
        store = "";
    }
    else
    {
        store = pub::getStr(store_depts, "^", pos + 1);
    }
    pos = pub::searchStr(drug_type, "^", ui->cbb_drug_type->currentText());

    if(pos == 0)
    {
        type = "";
    }
    else
    {
        type = pub::getStr(drug_type, "^", pos + 1);
    }

    sq = " delete from pha_sto_setdept "
         " where room_code = '%1' "
         " and drug_type = '%2' "
         " and dept_code = '%3'";

    sq = sq.arg(reci).arg(type).arg(store);

    qDebug()<<"移除可能存在的旧数据："<<sq<<"\n";

    qro->exec(sq);

    sq = " insert into pha_sto_setdept"
         " (room_code, drug_type, dept_code, "
         " oper_code, oper_date, begin_time)"
         " values('%1','%2','%3','%4',sysdate, sysdate)";
    sq = sq.arg(reci).arg(type).arg(store).arg(pub::usr_code);

    qDebug()<<"插入新数据："<<sq<<"\n";

    qro->exec(sq);

    on_pb_fresh_clicked();

}

void Tj_drug_consume_dept::on_pb_fresh_sp_clicked()
{
    qDebug()<<"\n\non_pb_fresh_sp_clicked()\n";
    sq = "SELECT dic_value, dic_key from drug_sub_dict where dic_type = 'leader'";

    md_sp->setQuery(sq, pub::db_o);

    ui->tv_list_sp->setModel(md_sp);

    int w = ui->tv_list_sp->width();

    pub::setColumnWidth(ui->tv_list_sp, "0.4^0.4^0^0^0", w);

}

void Tj_drug_consume_dept::on_tv_list_sp_doubleClicked(const QModelIndex &index)
{
    on_tv_list_sp_clicked(index);
}

void Tj_drug_consume_dept::on_tv_list_sp_clicked(const QModelIndex &index)
{
    ui->le_name_sp->setText(index.siblingAtColumn(0).data().toString());
    ui->le_code_sp->setText(index.siblingAtColumn(1).data().toString());
}

void Tj_drug_consume_dept::on_pb_delete_sp_clicked()
{
    qro->exec("delete from drug_sub_dict where dic_value = '"
              +ui->le_name_sp->text().trimmed()
              +"' and dic_type = 'leader'");
    on_pb_fresh_sp_clicked();
}

void Tj_drug_consume_dept::on_pb_add_sp_clicked()
{
    QString name = ui->le_name_sp->text().trimmed();
    QString code = ui->le_code_sp->text().trimmed();

    if(name.isEmpty() || code.isEmpty())
    {
        QMessageBox::information(this, "注意", "基础项目不可以空缺！");
        return;
    }


    qro->exec("insert into drug_sub_dict (dic_value, dic_key, dic_type ) "
              "values = '" + name + "','" + code + "','leader'");
    on_pb_fresh_sp_clicked();
}

void Tj_drug_consume_dept::on_tb_get_pic_file_clicked()
{

    QString file_name;

    file_name = QFileDialog::getOpenFileName(this,
                                             "打开签名图片文件",
                                             pub::downloadPath,
                                             "(*.png *.jpg)",  0);

    if(file_name.isNull())
    {
        return;
    }

    ui->le_pic_path->setText(file_name);

    // 加载图片
    QImage image(file_name);
    if (image.isNull())
    {
        QMessageBox::information(this, "注意", "文件格式无效！");
        return ;
    }

    QByteArray image_data;
    QBuffer buffer(&image_data);
    buffer.open(QIODevice::WriteOnly);

    // 保存为PNG格式到缓冲区
    if (image.save(&buffer, "PNG"))
    {
        // 转换为Base64
        img_base64 = image_data.toBase64();

    }
    else
    {
        QMessageBox::information(this, "注意", "文件格式转换失败！");
    }

    qDebug()<<"image_data.size():"<<QString::number(image_data.size());

    if(image_data.size() > 100000)
    {
        QMessageBox::information(this, "注意", "图像文件不允许超过 100k ！");
        ui->le_pic_path->setText("");
        return;
    }

    // 显示图片
    ui->lb_pic->setPixmap(QPixmap::fromImage(image));
    ui->lb_pic->setScaledContents(true);

}

void Tj_drug_consume_dept::on_cbb_dept_sign_currentTextChanged(const QString &arg1)
{

    if(!initial_finished)
    {
        return;
    }

    sq = "SELECT distinct priv.name , emp.empl_code"
         " FROM com_privileges priv"
         " join com_employee emp on emp.empl_name = priv.name"
         " where priv.role_dept = '"
         + arg1 + "' and priv.type = 'dept' ";

    doct_sign = pub::fillCombo(ui->cbb_name_sign, sq, -1);

}

void Tj_drug_consume_dept::on_pb_add_sign_clicked()
{

    QString name = ui->cbb_name_sign->currentText();
    QString dept = ui->cbb_dept_sign->currentText();

    if(ui->le_pic_path->text().isEmpty()
            || dept.isEmpty()
            || name.isEmpty())
    {
        QMessageBox::information(this, "注意", "基础信息不完整！");
        return;
    }

    // 如果需要完整的Base64字符串
    QString base64String = QString::fromLatin1(img_base64);
    QString dept_code, name_code;

    int pos = pub::searchStr(dept_sign, "^", dept);
    dept_code =  pub::getStr(dept_sign, "^", pos + 1) ;

    pos = pub::searchStr(doct_sign, "^", name);
        name_code =  pub::getStr(doct_sign, "^", pos + 1) ;

    sq = "select empl_name from com_empl_sign "
         " where empl_code = '" + name_code + "' ";
    qrm->exec(sq);
    if(qrm->next())
    {
        QMessageBox::StandardButton bt;
        bt = QMessageBox::question(this, "注意" , "用户名称 " + name + " 已经存在，是否覆盖？");
        if(bt != QMessageBox::Yes)
        {
            return;
        }
        qrm->exec("delete from com_empl_sign where empl_code = '" + name_code + "'");
    }

    sq = " insert into com_empl_sign "
         " (dept_code, dept_name, empl_code, empl_name, sign_pic, pic_idx)"
         " values "
         " ('%1','%2','%3','%4','%5',0)";
    sq = sq.arg(dept_code).arg(dept)
            .arg(name_code).arg(name)
            .arg(base64String);

    qrm->exec(sq);

    on_pb_fresh_sign_clicked();

}

void Tj_drug_consume_dept::on_pb_fresh_sign_clicked()
{

    sq = "select dept_name 科室, empl_name 姓名, empl_code"
         " from com_empl_sign "
         " order by dept_name, empl_name ";

    md_sign->setQuery(sq, pub::db_m);
    ui->tv_list_sign->setModel(md_sign);
    int w = ui->tv_list_sign->width();
    pub::setColumnWidth(ui->tv_list_sign, "0.4^0.4^0", w);

    ui->cbb_dept_sign->setCurrentIndex(-1);
    ui->cbb_name_sign->clear();
    QPixmap p;
    ui->lb_pic->setPixmap(p);

}

void Tj_drug_consume_dept::on_tv_list_sign_doubleClicked(const QModelIndex &index)
{
    on_tv_list_sign_clicked(index);
}

void Tj_drug_consume_dept::on_tv_list_sign_clicked(const QModelIndex &index)
{
    QString dept = index.siblingAtColumn(0).data().toString();
    QString name = index.siblingAtColumn(1).data().toString();
    QString code = index.siblingAtColumn(2).data().toString();

    ui->cbb_dept_sign->setCurrentText(dept);
    ui->cbb_name_sign->setCurrentText(name);

    qrm->exec("select sign_pic from com_empl_sign "
              " where empl_code = '" + code + "'");
    qrm->next();
    QString pic64 = qrm->value(0).toString();

    QByteArray image_data;
    QBuffer buffer(&image_data);
    buffer.open(QIODevice::WriteOnly);

    image_data = QByteArray::fromBase64(pic64.toLocal8Bit());

    QImage img = QImage::fromData(image_data, "PNG");

    // 显示图片
    ui->lb_pic->setPixmap(QPixmap::fromImage(img));
    ui->lb_pic->setScaledContents(true);

}

void Tj_drug_consume_dept::on_pb_delete_sign_clicked()
{
    if(!ui->tv_list_sign->currentIndex().isValid())
    {
        QMessageBox::information(this, "注意", "未指定有效用户信息！");
        return;
    }

    QString code, name;
    code = ui->tv_list_sign->currentIndex().siblingAtColumn(2).data().toString();
    name = ui->tv_list_sign->currentIndex().siblingAtColumn(1).data().toString();

    QMessageBox::StandardButton bt;
    bt = QMessageBox::question(this, "注意" , "确定要移除用户 " + name + " 的签字图像信息？");
    if(bt != QMessageBox::Yes)
    {
        return;
    }


    qrm->exec("delete from com_empl_sign where empl_code = '" + code + "'");

    on_pb_fresh_sign_clicked();
}

void Tj_drug_consume_dept::on_pb_fresh_pic_clicked()
{

    QString card_no = ui->le_card_no_pic->text().trimmed();

    if(card_no.isEmpty())
    {
        return;
    }

    card_no = pub::add0(card_no,10);

    sq = " select name 姓名,dept_name 诊室,to_char(reg_date, 'yyyy-mm-dd') 挂号日期," //0-2
         " card_no 病志号, doct_code,doct_name,see_docd,clinic_code"//3-7
         " from fin_opr_register"
         " where card_no = '%1'"
         " order by reg_date";
    sq = sq.arg(card_no);

    qDebug()<<"检索指定病志号："<<sq<<"\n";

    md_pic->setQuery(sq, pub::db_o);

    if(md_pic->rowCount() < 1)
    {

        QMessageBox::warning(this, "注意", "输入的病志号无效！");
        return;
    }

    ui->tv_list_card_no_pic->setModel(md_pic);
    int w = ui->tv_list_card_no_pic->width();
    pub::setColumnWidth(ui->tv_list_card_no_pic, "^0.4^0.4^0^0^0^0^0^0^0^0",w);


}

void Tj_drug_consume_dept::on_tv_list_card_no_pic_doubleClicked(const QModelIndex &index)
{
    on_tv_list_card_no_pic_clicked(index);
}

void Tj_drug_consume_dept::on_tv_list_card_no_pic_clicked(const QModelIndex &index)
{

    QString see_docd = index.siblingAtColumn(6).data().toString();

    pic_patient_name = index.siblingAtColumn(0).data().toString();
    pic_dept = index.siblingAtColumn(1).data().toString();
    pic_date = index.siblingAtColumn(2).data().toString();
    pic_patient_code = index.siblingAtColumn(3).data().toString();
    pic_doct_code = index.siblingAtColumn(4).data().toString();
    pic_doct_name = index.siblingAtColumn(5).data().toString();
    pic_clinic_code = index.siblingAtColumn(7).data().toString();

    if(see_docd != pic_doct_code)
    {
        //挂号与接诊不一致
        sq = "select empl_name from com_employee where empl_code = '" + see_docd + "'";
        qro->exec(sq);
        qro->next();
        pic_doct_name = qro->value(0).toString();
        pic_doct_code = see_docd;
    }

    ui->le_dept_pic->setText(pic_dept);
    ui->le_reg_date_pic->setText(pic_date);
    ui->le_patient_name_pic->setText(pic_patient_name);
    ui->le_doct_pic->setText(pic_doct_name);

}

void Tj_drug_consume_dept::on_tb_get_pic_file_pic_clicked()
{

    QString file_name;

    file_name = QFileDialog::getOpenFileName(this,
                                             "打开图片文件",
                                             "",
                                             "(*.jpg)",  0);

    if(file_name.isNull())
    {
        return;
    }

    ui->le_pic_path_pic->setText(file_name);

    // 加载图片
    QImage image(file_name);
    if (image.isNull())
    {
        QMessageBox::information(this, "注意", "文件格式无效！");
        return ;
    }

    QByteArray image_data;
    QBuffer buffer(&image_data);
    buffer.open(QIODevice::WriteOnly);

    // 保存为PNG格式到缓冲区
    if (image.save(&buffer, "PNG"))
    {
        // 转换为Base64
        img_base64 = image_data.toBase64();

    }
    else
    {
        QMessageBox::information(this, "注意", "文件格式转换失败！");
    }

    QFile f(file_name);

    qDebug()<<"image_data.size():"<<QString::number(f.size());

    if(f.size() > 1000000)
    {
        QMessageBox::information(this, "注意", "图像文件不允许超过 1M ！");
        ui->le_pic_path_pic->setText("");
        return;
    }

}

void Tj_drug_consume_dept::on_pb_upload_pic_clicked()
{
    qDebug()<<"on_pb_pic_upload_clicked\n";

    QString pic_file = ui->le_pic_path_pic->text();

    if(pic_file.isEmpty() || pic_patient_code.isEmpty())
    {
        QMessageBox::information(this, "注意", "基础数据不完整 ！");
        return;
    }

    QDir d = QDir(pic_path);

    QString dest_path = pic_path + "/" + pic_patient_code;
    QDir dest_d = QDir();

    if(!dest_d.exists(dest_path))
    {
        dest_d.mkdir(dest_path);
    }

    QString pic_sq;

    QString tm_mark = QDateTime::currentDateTime().toString("yyyyMMddHHmmsszzz");
    QString dest_filename = pic_patient_code + "-" + tm_mark + ".jpg";

    if(!QFile::copy(pic_file, dest_path + "/" + dest_filename))
    {
        QMessageBox::information(this, "注意", "本地文件拷贝失败 ！");
        return;
    }

    //QFile f(pic_file);
    //f.copy(dest_path + "/" + dest_filename);

    pic_sq = "insert into ys_bz_e_pic "
             "(oper_date, card_no, pic_name, "
             "doc_name, doc_code, pic_describe, clinic_code, "
             "patient_name, reg_date, tm_mark, sync_flag) "
             "values"
             "(curdate(), '%1', '%2', "     //card_no, dest_filename
            "'%3', '%4', '', '%5', " //doc_name,doc_code, file_describe, clinic_code
            "'%6', '%7', '%8','1')";//patietn_name, reg_date, tm_mark

    pic_sq = pic_sq.arg(pic_patient_code)
            .arg(dest_filename)
            .arg(pic_doct_name)
            .arg(pic_doct_code)
            .arg(pic_clinic_code)
            .arg(pic_patient_name)
            .arg(pic_date)
            .arg(tm_mark);

    qDebug()<<"保存图像记录信息\n"<<pic_sq;

    if(!qrm->exec(pic_sq))
    {
        QMessageBox::warning(this, "Note", "图像信息保存失败！");
        return;
    }

    qDebug()<<"remote_file_syncronize\n";

    //QFile fl;

    QString remote_dest_path = remote_path + "/" + pic_patient_code;

    if(!dest_d.exists(remote_dest_path))
    {
        dest_d.mkdir(remote_dest_path);
    }

    QString remote_name = remote_dest_path + "/" + dest_filename;

    //QString sour = file_path + "/" + dest_filename;
    QString dest = remote_dest_path + "/" + dest_filename;
    qDebug()<<"source : "<<pic_file;
    qDebug()<<"destination : "<<dest;
    if(!QFile::copy(pic_file, dest))
    {
        QMessageBox::information(this, "注意", "选程文件拷贝失败 ！");
        return;
    }

    QMessageBox::warning(this, "Note", "图像上传保存完成！");
    ui->le_pic_path_pic->clear();

}

void Tj_drug_consume_dept::on_pb_med_rec_save_clicked()
{

    if(ui->lw_med_item->count() < 1)
    {
        QMessageBox::information(this, "注意", "未选定病志打印项目 ！");
        return;
    }

    QString doc_type = ui->cbb_doct_type->currentText();

    if(doc_type.isEmpty())
    {
        QMessageBox::warning(this, "Note", "请先选定一个医生类别！");
        return;
    }

    sq = "delete from ys_bz_e_print_rule"
         " where doct_type = '" + doc_type + "' order by print_seq";
    qrm->exec(sq);

    for(int i = 0; i < ui->lw_med_item->count(); i++)
    {
        QString itm = ui->lw_med_item->item(i)->text();
        sq = "insert into ys_bz_e_print_rule "
             "(oper_date,oper_code,doct_type,med_rec_item,print_seq) "
             " select sysdate(),'%1','%2','%3',print_seq"
             " from ys_bz_e_print_rule "
             " where doct_type = 'ALL' and med_rec_item = '%3'";
        sq = sq.arg(pub::usr_code)
                .arg(doc_type)
                .arg(itm);

        qDebug()<<"插入指定医师类别的病志内容：";
        qDebug()<<sq;
        qrm->exec(sq);
    }

    QMessageBox::warning(this, "Note", "病志打印项目保存完成 ！");

}

void Tj_drug_consume_dept::on_cbb_doct_type_currentTextChanged(const QString &arg1)
{
    if(!initial_finished)
    {
        return;
    }

    sq = "select med_rec_item from ys_bz_e_print_rule"
         " where doct_type = '" + arg1 + "' order by print_seq";

    qrm->exec(sq);
    ui->lw_med_item->clear();
    while(qrm->next())
    {
        ui->lw_med_item->addItem(qrm->value(0).toString());
    }
}

void Tj_drug_consume_dept::on_lw_med_item_all_doubleClicked(const QModelIndex &index)
{

    if(ui->cbb_doct_type->currentText().isEmpty())
    {
        QMessageBox::warning(this, "Note", "请先选定一个医生类别！");
        return;
    }

    QString itm = index.data().toString();
    QList<QListWidgetItem *>lst = ui->lw_med_item->findItems(itm, Qt::MatchExactly);

    if(lst.size() > 0)
    {
        QMessageBox::warning(this, "Note", "项目已经存在！");
        return;
    }

    ui->lw_med_item->addItem(itm);

}

void Tj_drug_consume_dept::on_lw_med_item_doubleClicked(const QModelIndex &index)
{

    ui->lw_med_item->takeItem(index.row());

}

void Tj_drug_consume_dept::on_tabWidget_currentChanged(int index)
{

}
